package com.gosophia.metadataEngine.service.impl.test.integration;

import java.util.List;
import java.util.UUID;

import junit.framework.Assert;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;

import com.gosophia.commons.Page;
import com.gosophia.commons.exception.BusinessException;
import com.gosophia.commons.exception.ConcurrencyException;
import com.gosophia.metadataEngine.dao.DataSourceInfoDao;
import com.gosophia.metadataEngine.entity.DataSourceInfo;
import com.gosophia.metadataEngine.service.DataSourceInfoService;
/**
 * DataSourceInfoService集成测试
 * @author mayue
 *
 */
@ContextConfiguration(locations = { "classpath:applicationContext-it.xml",
"classpath*:moduleContext.xml" })
@TransactionConfiguration(defaultRollback=true)
public class ITDataSourceInfoServiceImpl extends
        AbstractTransactionalJUnit4SpringContextTests {
    @Autowired
    private DataSourceInfoService dataSourceInfoService;
    @Autowired
    private DataSourceInfoDao dataSourceInfoDao;
    @Test
    public void testAddDataSourceInfo() throws BusinessException{
        String name = "名字"+UUID.randomUUID().toString();
        DataSourceInfo dsi = new DataSourceInfo();
        dsi.setDialect("sql");
        dsi.setDriverClass("driverclass");
        dsi.setName(name);
        dsi.setPassword("124");
        dsi.setUrl("url");
        dsi.setUsername("userName");
        dataSourceInfoService.addDataSourceInfo(dsi);
        List<DataSourceInfo> list = dataSourceInfoDao.getByProperty("name", name);
        Assert.assertEquals(1, list.size());
        
    }
    @Test(expected = BusinessException.class)
    public void testAddDataSourceInfoRepeatName() throws BusinessException{
        String name = "数据源1";
        DataSourceInfo dsi = new DataSourceInfo();
        dsi.setDialect("sql");
        dsi.setDriverClass("driverclass");
        dsi.setName(name);
        dsi.setPassword("124");
        dsi.setUrl("url");
        dsi.setUsername("userName");
        dataSourceInfoService.addDataSourceInfo(dsi);
    }
    @Test
    public void testRemoveDataSourceInfo() throws BusinessException{
    	//TODO 未通过
        dataSourceInfoService.removeDataSourceInfo(2L,0L);
    }
    
    @Test(expected = ConcurrencyException.class)
    public void testRemoveDataSourceInfoNoId() throws BusinessException{
        dataSourceInfoService.removeDataSourceInfo(10000L, 0L);
    }
    
    @Test(expected = ConcurrencyException.class)
    public void testRemoveDataSourceInfoVersionChange() throws BusinessException{
        dataSourceInfoService.removeDataSourceInfo(2L, 10L);
    }
    @Test(expected = BusinessException.class)
    public void testRemoveDataSourceInfoIsUsed() throws BusinessException{
        dataSourceInfoService.removeDataSourceInfo(1L,0L);
    }
    @Test
    public void testUpdateDataSourceInfo() throws BusinessException{
        String name ="修改后的名字"+UUID.randomUUID().toString();
        DataSourceInfo dsi = dataSourceInfoDao.get(2L);
        dsi.setName(name);
        dataSourceInfoService.updateDataSourceInfo(dsi);
        dataSourceInfoDao.clearEntityOnSession(dsi);
        DataSourceInfo dsi1 = dataSourceInfoDao.get(2L);
        Assert.assertEquals(name, dsi1.getName());
        
    }
    @Test
    public void testUpdateDataSourceInfoSelf() throws BusinessException{
        String name ="重命名";
        DataSourceInfo dsi = dataSourceInfoDao.get(2L);
        dsi.setName(name);
        dataSourceInfoService.updateDataSourceInfo(dsi);
        dataSourceInfoDao.clearEntityOnSession(dsi);
        DataSourceInfo dsi1 = dataSourceInfoDao.get(2L);
        Assert.assertEquals(name, dsi1.getName());
    }
    @Test(expected = BusinessException.class)
    public void testUpdateDataSourceInfoRepeat() throws BusinessException{
        String name ="数据源1";
        DataSourceInfo dsi = dataSourceInfoDao.get(2L);
        dsi.setName(name);
        dataSourceInfoService.updateDataSourceInfo(dsi);
    }
    @Test
    public void testFindDataSourceInfoById(){
        DataSourceInfo dsi = dataSourceInfoService.findDataSourceInfoById(1L);
        Assert.assertEquals("数据源1", dsi.getName());
    }
    @Test
    public void testFindAllDataSourceInfoByPage(){
    	Page<DataSourceInfo> page = new Page<DataSourceInfo>();
    	page.setPageNo(1);
    	page.setPageSize(10);
        page = dataSourceInfoService.findAllDataSourceInfoByPage(page,null,null);
        Assert.assertEquals(2, page.getTotalCount());
    }

}
